import Vue from 'vue';
import Router from 'vue-router';

import baseRouters from './modules/base';

// const env = 'development';

// 存放动态路由
export const asyncRouterList = [...baseRouters];

// 存放固定的路由
const defaultRouterList = [
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/index.vue'),
  },
  {
    path: '*',
    // redirect: '/dashboard/base',
    redirect: '/bi/data-analysis',
  },
  ...asyncRouterList,
];

// hack router push callback
const originalPush = Router.prototype.push;
Router.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject);
  return originalPush.call(this, location).catch((err) => err);
};

Vue.use(Router);

const createRouter = () =>
  new Router({
		base: process.env.VUE_APP_BASE_URL,
    mode: 'hash',
    routes: defaultRouterList,
    scrollBehavior() {
      return { x: 0, y: 0 };
    },
  });

const router = createRouter();

export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
